﻿using Microsoft.Practices.Unity.InterceptionExtension;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace NCS.IConnect.Core.EntLib.IoC
{
    /// <summary>
    /// This matching rule is used for matching base type.
    /// </summary>
    public class BaseTypeMatchingRule :IMatchingRule
    {
        private string baseTypeAssemblyQualifiedName;

        /// <summary>
        /// Initializes a new instance of the <see cref="BaseTypeMatchingRule"/> class.
        /// </summary>
        /// <param name="baseTypeAssemblyQualifiedName">Name of the base type assembly qualified.</param>
        public BaseTypeMatchingRule(string baseTypeAssemblyQualifiedName)
        {
            this.baseTypeAssemblyQualifiedName = baseTypeAssemblyQualifiedName;
        }

        /// <summary>
        /// Tests to see if this rule applies to the given member.
        /// </summary>
        /// <param name="member">Member to test.</param>
        /// <returns>
        /// true if the rule applies, false if it doesn't.
        /// </returns>
        public bool Matches(MethodBase member)
        {
            var type = Type.ReflectionOnlyGetType(this.baseTypeAssemblyQualifiedName, false, true);
            if (type == null) return false;
            return type.IsAssignableFrom(member.GetType());
        }
    }
}
